<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>mpa and reduce</title>
</head>
<body>
    <script>
        
        'use strict';

        // 1.map
        function pow(x){
            return x*x;
        }

        var arr = [1,2,3,4,5,6,7,8,9,10];
        var results1 = arr.map((x)=>(x*x));
        console.log(results1);

        var results2 = arr.map(String);
        console.log(results2);


        // 2.reduce
        var arr2 = [1,3,5,7,9];
        var sum2 = arr2.reduce(function(x,y){
            return x + y
        });
        console.log('sum1 = ' + sum2);

        // 2.1 Array求和
        var arr3 = [1,3,5,7,9];
        var num3 = arr3.reduce((x,y)=>(x * 10 + y));
        console.log(num3);
        
        // 2.2 String to Int
        function string2int(s){
            return s.split('').map((x)=>(x*1)).reduce((x,y)=>(x* 10 + y));
        }
        console.log(string2int('123456'));

        // 2.3 请把用户输入的不规范的英文名字，变为首字母大写，其他小写的规范名字。
        //输入：['adam', 'LISA', 'barT']，输出：['Adam', 'Lisa', 'Bart']。
        function normalize(arr){
            return arr.map((x)=>x[0].toUpperCase()+x.substring(1).toLowerCase());
        }
        console.log(normalize(['adam', 'LISA', 'barT']));

        // 2.4 利用map()把字符串变成整数
        var arr4 = ['1','2','3','4','5'];
        var r;
        r = arr4.map(Number);
        console.log(r);
    </script>
</body>
</html>    